using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace Library_management_system
{
    public partial class BookManagementForm : Form
    {
        public BookManagementForm()
        {
            InitializeComponent();
            LoadBooks();
        }

        private void LoadBooks(string keyword = "")
        {
            using (SqlConnection conn = DatabaseConfig.GetConnection())
            {
                conn.Open();
                string sql = "SELECT BookID, Title, Author, Publisher, ISBN, PublishDate, Category, TotalCount, AvailableCount FROM Books";
                if (!string.IsNullOrWhiteSpace(keyword))
                {
                    sql += " WHERE Title LIKE @kw OR Author LIKE @kw OR ISBN LIKE @kw OR Category LIKE @kw";
                }
                SqlCommand cmd = new SqlCommand(sql, conn);
                if (!string.IsNullOrWhiteSpace(keyword))
                {
                    cmd.Parameters.AddWithValue("@kw", "%" + keyword + "%");
                }
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable();
                da.Fill(dt);
                dgvBooks.DataSource = dt;
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            LoadBooks(txtSearch.Text.Trim());
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            BookEditForm editForm = new BookEditForm();
            if (editForm.ShowDialog() == DialogResult.OK)
            {
                LoadBooks();
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (dgvBooks.SelectedRows.Count == 0)
            {
                MessageBox.Show("请选择要修改的图书！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            int bookId = Convert.ToInt32(dgvBooks.SelectedRows[0].Cells["BookID"].Value);
            BookEditForm editForm = new BookEditForm(bookId);
            if (editForm.ShowDialog() == DialogResult.OK)
            {
                LoadBooks();
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (dgvBooks.SelectedRows.Count == 0)
            {
                MessageBox.Show("请选择要删除的图书！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            int bookId = Convert.ToInt32(dgvBooks.SelectedRows[0].Cells["BookID"].Value);
            if (MessageBox.Show("确定要删除该图书吗？", "确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                using (SqlConnection conn = DatabaseConfig.GetConnection())
                {
                    conn.Open();
                    string sql = "DELETE FROM Books WHERE BookID=@id";
                    SqlCommand cmd = new SqlCommand(sql, conn);
                    cmd.Parameters.AddWithValue("@id", bookId);
                    cmd.ExecuteNonQuery();
                }
                LoadBooks();
            }
        }
    }
}